package com.dsteiner.xelavos.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dsteiner.widgets.client.StringUtil;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.server.dao.DataDao;

public class EmailAdminServlet extends HttpServlet {

	private DataDao dataDao = DataDao.dataDao;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/plain");
		try {
			String removeClassName = req.getParameter("remove");
			String cancelWeeklyReport = req.getParameter("cancelWeeklyReport");
			if(!StringUtil.isEmpty(removeClassName)){
				String id = req.getParameter("id");
				assert id != null;
				Class<? extends ModelObject> forName = (Class<? extends ModelObject>) Class.forName(removeClassName);
				dataDao.deleteObject(id, forName);
			}
			if(!StringUtil.isEmpty(cancelWeeklyReport)){
				String userId = req.getParameter("uid");
				User user = (User) dataDao.getObjectById(userId, User.class);
				user.setReceiveReport(false);
				dataDao.updateObject(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		res.sendRedirect("http://www.xelavos.com");
	}
}